/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.WeatherDao;
import dao.WeatherDaoImpl;
import entities1.WeatherInformation;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author takamflore
 */
public class WeatherServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
    String forward="";
    @EJB
    WeatherDao weatherDao;
    List<WeatherInformation> weathers;
    public WeatherServlet(){
        super();
        weatherDao=new WeatherDaoImpl();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
      
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String action= request.getParameter("action");
       WeatherInformation weath;
       int id= Integer.parseInt(request.getParameter("weatherid"));
       if(action.equalsIgnoreCase("View")){
           weath= weatherDao.checkWeatherInformationID(id);
           request.setAttribute("weather",weath);
       }else{
          weathers=weatherDao.allWeaterInformation();
            request.setAttribute("weathers", weathers);
            forward="/WEB-INF/entity/weather/List_Weather.jsp";
       }
        
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        WeatherInformation weat= new WeatherInformation();
       weat.setWeather(request.getParameter("weather"));
       weat.setTemperature(request.getParameter("temperature"));
       weat.setPrecipitation(request.getParameter("precipitation"));
       weat.setCity(request.getParameter("city"));
       weatherDao.addWeatherInfo(weat);
        RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/entity/weather/View.jsp");
        request.setAttribute("user",weat);
        view.forward(request,response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
